package com.cuit.qx.service.impl;

import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSONUtil;
import com.cuit.qx.entity.Config;
import com.cuit.qx.mapper.ConfigMapper;
import com.cuit.qx.service.ConfigService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.cuit.qx.utils.JsonUtil;
import com.cuit.qx.utils.PatternUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.stream.Collectors;

/**
 * <p>
 * 系统配置表 服务实现类
 * </p>
 *
 * @author lcj
 * @since 2021-11-23
 */
@Service
public class ConfigServiceImpl extends ServiceImpl<ConfigMapper, Config> implements ConfigService {

    @Resource
    private PatternUtils patternUtils;

    @Override
    public Map<String, Object> getData() {
        List<Config> configs = this.baseMapper.selectList(null);
        return configs.stream().collect(Collectors.toMap(e -> patternUtils.lineToHump(e.getConfigKey()) +
                        (e.getConfigType().equals("0")?"":e.getConfigType()),
                e -> JSONUtil.isJson(String.valueOf(e.getConfigContent())) ?
                        JSONUtil.parse(String.valueOf(e.getConfigContent())) :
                        Optional.ofNullable(e.getConfigContent()).orElse("")));
    }
}
